package com.ywicc.usblibs.comm;

/**
 * author : zhouxuan77
 * e-mail : zhouxuan77@163.com
 * time   : 2022/03/27
 * desc   : 标志物通讯协议封装
 * version: V4.3
 */
public class CommMarkersUtils {

    public final static int TYPE_BARRIER_GATE = 0X03;
    public final static int TYPE_LED = 0x04;
    public final static int TYPE_WIRELESS_CHARGING = 0x0A;
    public final static int TYPE_TFT_A = 0x0B;
    public final static int TYPE_TFT_B = 0x08;
    public final static int TYPE_STEREO_GARAGE_A = 0x0D;
    public final static int TYPE_STEREO_GARAGE_B = 0x05;
    public final static int TYPE_TRAFFIC_LIGHT_A = 0x0E;
    public final static int TYPE_TRAFFIC_LIGHT_B = 0x0F;
    public final static int TYPE_BEACON_TOWER = 0x07;

    public final static int BARRIER_GATE_ON = 0x01;
    public final static int BARRIER_GATE_OFF = 0x02;
    public final static int BARRIER_GATE_FRONT_PLATE_NUMBER = 0X10;
    public final static int BARRIER_GATE_BEHIND_PLATE_NUMBER = 0X11;
    public final static int BARRIER_GATE_BACK_DATA = 0x20;

    public final static int LED_FIRST_DATA = 0x01;
    public final static int LED_SECOND_DATA = 0x02;
    public final static int LED_START_COUNTDOWN = 0x03;
    public final static int LED_SHOW_DISTANCE = 0x04;

    public final static int WIRELESS_CHARGING_ON = 0x01;

    public final static int TFT_SHOW_PHOTO = 0x10;
    public final static int TFT_SHOW_PLATE_NUMBER_A = 0x20;
    public final static int TFT_SHOW_PLATE_NUMBER_B = 0x21;
    public final static int TFT_TIMING = 0x30;
    public final static int TFT_HEX = 0x40;
    public final static int TFT_DISTANCE = 0x50;

    public final static int STEREO_GARAGE_CONTROL = 0x01;
    public final static int STEREO_GARAGE_BACK_DATA = 0x02;

    public final static int TRAFFIC_LIGHT_CHECK = 0x01;
    public final static int TRAFFIC_LIGHT_BACK_DATA = 0x02;

    public final static int BEACON_TOWER_FRONT_DATA = 0x10;
    public final static int BEACON_TOWER_BEHIND_DATA = 0x11;

    public byte[] getMarkersData(int object, int type, int first, int second, int third) {
        byte checkData = (byte) ((type + first + second + third) % 256);

        return new byte[]{0x55, (byte) object, (byte) type, (byte) first, (byte) second, (byte) third, checkData, (byte) 0xBB};
    }

    public byte[] getCustomData(int object, int type, int first, int second, int third) {
        byte checkData = (byte) ((type + first + second + third) % 256);

        return new byte[]{0x55, (byte) object, (byte) type, (byte) first, (byte) second, (byte) third, checkData, (byte) 0xBB};
    }

    public String[] getCarState(byte[] data) {
        if (data[0] != 0x55) {
            return null;
        }

        String[] strDates = new String[6];

        switch (data[2]) {
            case 0x00:
                strDates[0] = "循迹状态";
                break;
            case 0x01:
                strDates[0] = "十字路口状态";
                break;
            case 0x02:
                strDates[0] = "转弯完成";
                break;
            case 0x03:
                strDates[0] = "前进、后退完成";
                break;
            case 0x04:
                strDates[0] = "出循迹线";
                break;
            case 0x05:
                strDates[0] = "道闸打卡";
                break;
            case 0x4A:
                strDates[0] = "语音芯片初始化";
                break;
            case 0x41:
                strDates[0] = "语音芯片正确帧";
                break;
            case 0x45:
                strDates[0] = "语音芯片错误帧";
                break;
            case 0x4E:
                strDates[0] = "语音芯片合成中";
                break;
            case 0x4F:
                strDates[0] = "语音芯片空闲";
                break;
            case 0x06:
                strDates[0] = "ETC 打开成功";
                break;
            case 0x07:
                strDates[0] = "交通灯识别模式";
                break;
            case 0x08:
                strDates[0] = "未能进入交通灯识别模式";
                break;
            case 0x09:
                strDates[0] = "立体车库 第一层";
                break;
            case 0x0A:
                strDates[0] = "立体车库 第二层";
                break;
            case 0x0B:
                strDates[0] = "立体车库 第三层";
                break;
            case 0x0C:
                strDates[0] = "立体车库 第四层";
                break;
            case 0x11:
                strDates[0] = "立体车库红外触发";
                break;
            case 0x22:
                strDates[0] = "立体车库红外未触发";
                break;
            case 0x12:
                strDates[0] = "立体车库前红外触发";
                break;
            case 0x21:
                strDates[0] = "立体车库后红外触发";
                break;
            default:
                strDates[0] = Integer.toHexString(data[2]);
                break;
        }

        long ultrasonic = data[5] & 0xff;
        ultrasonic = ultrasonic << 8;
        ultrasonic += data[4] & 0xff;

        long photosensitive = data[3] & 0xff;

        long light = data[7] & 0xff;
        light = light << 8;
        light += data[6] & 0xff;

        long encoder = data[9] & 0xff;
        encoder = encoder << 8;
        encoder += data[8] & 0xff;

        strDates[1] = String.valueOf(photosensitive);
        strDates[2] = ultrasonic + " mm";
        strDates[3] = String.valueOf(light);
        strDates[4] = String.valueOf(encoder);
        strDates[5] = "未开发";

        return strDates;
    }

}
